package com.lams.util;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import com.lams.jdbc.IBaseDAO;
import com.lams.jdbc.QueryUtils;

public class IdFactory {
	private static String nextnav ;
	private static String updateCurrnav;
	private static String currnav;
	private static IBaseDAO dao;
	
	public void setDao(IBaseDAO dao){
		IdFactory.dao = dao;
	}
	
	public void setNextnav(String nextnav) {
		IdFactory.nextnav = nextnav;
	}

	public void setUpdateCurrnav(String updateCurrnav) {
		IdFactory.updateCurrnav = updateCurrnav;
	}

	public void setCurrnav(String currnav) {
		IdFactory.currnav = currnav;
	}

	/**
	 * 取新ID
	 * @param tab_name 表名
	 * @param column 字段名
	 * @return
	 */
	public static int getNextNav(String tab_name,String column){
		Map params = new HashMap();
		params.put("TAB_NAME", tab_name);
		params.put("COLUMN_NAME", column);
		String sql = QueryUtils.getSqlBySqlCode(IdFactory.nextnav, params);
		Map map = QueryUtils.executeRecord(sql);
		int nextNav = Integer.parseInt(String.valueOf(map.get("NEXTNAV")));
		sql = QueryUtils.getSqlBySqlCode(IdFactory.updateCurrnav, params);
		//System.out.println("sql:"+sql);
		try {
			dao.executeUpdate(sql);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return nextNav;
	}
	public static int getCurrNav(String tab_name,String column){
		Map params = new HashMap();
		params.put("TAB_NAME", tab_name);
		params.put("COLUMN_NAME", column);
		String sql = QueryUtils.getSqlBySqlCode(IdFactory.currnav, params);
		Map map = QueryUtils.executeRecord(sql);
		int currNav = Integer.parseInt(String.valueOf(map.get("CURRNAV")));
		return currNav;
	}
//	public static void main(String[] args) {
//		Map params = new HashMap();
//		params.put("TAB_NAME", "c_student");
//		params.put("COLUMN_NAME", "student_id");
//		String sql = QueryUtils.getSqlBySqlCode(IdFactory.nextnav, params);
//		System.out.println("sql:"+sql);
//		Map map = QueryUtils.executeRecord(sql);
//		System.out.println(map.toString());
//		sql = QueryUtils.getSqlBySqlCode(IdFactory.currnav, params);
//		System.out.println("sql:"+sql);
//		int flag = -1;
//		try {
//			flag = dao.executeUpdate(sql);
//		} catch (SQLException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//		System.out.println(flag);
//		sql = QueryUtils.getSqlBySqlCode(IdFactory.nextnav, params);
//		System.out.println("sql:"+sql);
//		map = QueryUtils.executeRecord(sql);
//		System.out.println(map.toString());
//	}
}
